from flask import render_template,redirect,request
from . import api_0_4

from .spider.login_spider import login_hlju
from .spider.userinfo_spider import request_userinfo
from .spider.courses_spider import request_courses
from .spider.scores_spider import request_scores
from .spider.credit_spider import request_credit
from .spider.exams_spider import request_exams
from .spider.result import result
from ..models import News

from .common import read_version


@api_0_4.route('/login',methods=['GET','POST'])
def login():
    if request.method=='GET':
        return result(102)
    try:
        return login_hlju(request.form['uid'],request.form['pwd'])
    except KeyError as e:
        return result(103)

@api_0_4.route('/userinfo',methods=['GET','POST'])
def userinfo():
    if request.method=='GET':
        return result(102)
    try:
        return request_userinfo(request.form['uid'],request.form['token'])
    except KeyError as e:
        return result(103)


@api_0_4.route('/credit',methods=['GET','POST'])
def credit():
    if request.method=='GET':
        return result(102)
    try:
        return request_credit(request.form['uid'],request.form['token'])
    except KeyError as e:
        return result(103)


@api_0_4.route('/courses',methods=['GET','POST'])
def courses():
    if request.method=='GET':
        return result(102)
    try:
        return request_courses(request.form['uid'],request.form['token'])
    except KeyError as e:
        return result(103)


@api_0_4.route('/scores',methods=['GET','POST'])
def scores():
    if request.method=='GET':
        return result(102)
    try:
        return request_scores(request.form['uid'],request.form['token'])
    except KeyError as e:
        return result(103)


@api_0_4.route('/exams',methods=['GET','POST'])
def exams():
    if request.method=='GET':
        return result(102)
    try:
        return request_exams(request.form['uid'],request.form['token'])
    except KeyError as e:
        return result(103)

@api_0_4.route('/newslist',methods=['GET','POST'])
def newslist():
    if request.method=='GET':
        page = request.args.get('page',1,type=int)
        size = request.args.get('size',10)
    else:
        page = int(request.form['page'])
        size = request.form['size']
    # 按照发布时间降序排序
    pagination = News.query.order_by(-News.pubTime).paginate(page,per_page=size,error_out=False)

    newslist = pagination.items
    if len(newslist) == 0:
        return result(109)
    return result(100,[news.to_json_list() for news in newslist])

@api_0_4.route('/newsdetail',methods=['GET','POST'])
def newsdetail():

    if request.method=='GET':
        nid = request.args.get('nid',0,type=int)
    else:
        nid = int(request.form['nid'])

    if nid == 0:
        return result(103)

    news = News.query.filter_by(nid=nid).first()
    if news == None:
        return result(109)
    return result(100,[news.to_json_detail()])

@api_0_4.route('/checkupdate',methods=['GET','POST'])
def check_update():
    try:
        device = request.form['device']
        versionCode = int(request.form['versionCode'])
        return result(100,[read_version(device,versionCode)])

    except KeyError as e:
        return result(103)
